function gamemap(mapFile){
    this.tileHeight = 32;
    this.tileWidth = 64;
    this.height;
    this.width;
    this.editFloor;
    this.zoom = 1;
    this.editGrid;
    this.editGridScale = 0.5;
}
gamemap.prototype.load = function(){
    /**
     * Background
     */
    $('#pageContainer').css("width", window.innerWidth).css("height",window.innerHeight);
    $(document).on("scroll",function(){
        $('body').scrollTop(0).scrollLeft(0);
    })
    $(document).attr('unselectable', 'on').css('user-select', 'none').on('selectstart', false);

    //Main container
    game.mainContainer = new createjs.Container();
    game.stage.addChild(game.mainContainer);
    game.mainContainer.x = game.mainContainer.y = 0;
    //Editor container
    game.editorContainer = new createjs.Container();
    game.stage.addChild(game.editorContainer);
    game.editorContainer.x = game.editorContainer.y = 0;
    game.editorContainer.zIndex = 100;

    game.gameMap.initEditor();
}
gamemap.prototype.initEditor = function(){
    /**
     * Global
     */
    game.mainContainer.width = 1000;
    game.mainContainer.height = 500;

    game.mainContainer.regX = - ((window.innerWidth/2) >> 0);
    game.mainContainer.regY = - ((window.innerHeight/2) >> 0);


    $('#gameBackground').bind('mousewheel DOMMouseScroll', function(evt){
        evt.preventDefault();
        if (evt.originalEvent.wheelDelta > 0 || evt.originalEvent.detail < 0) {
            //zoom out
            if(game.gameMap.zoom > 1){
                game.gameMap.zoom--;
            }
        }else{
            //zoom in
            if(game.gameMap.zoom < 3){
                game.gameMap.zoom++;
            }
        }
        switch(game.gameMap.zoom){
            case 1:
                game.mainContainer.scaleX = 1;
                game.mainContainer.scaleY = 1;
                break;

            case 2:
                game.mainContainer.scaleX = 2;
                game.mainContainer.scaleY = 2;
                break;

            case 3:
                game.mainContainer.scaleX = 3;
                game.mainContainer.scaleY = 3;
                break;
        }
        $('#gameBackground').attr('class', "zoom"+game.gameMap.zoom);
        game.gameMap.moveToCenter();
    });
    $(document).bind('keyup', function(evt){
        var key = evt.keyCode;
        if(game.gameGui.constructMode == true && key == 32){
            evt.preventDefault();
            game.gameGui.rotateConstruct(evt);
        }
    });
    $('#guiTop, #guiBottom').bind('mouseover',function(evt){
        $('#gamemap').off("mousemove");
    }).bind('contextmenu',function(evt){
        evt.preventDefault();
    });

    game.gameRenderer.stopRendering = false;
}
gamemap.prototype.createInitTiles = function(size){
    if(game.gameMap.editFloor != null){
        game.mainContainer.removeChild(game.gameMap.editFloor);
        delete(game.gameMap.editFloor);
    }
    game.gameMap.editFloor = new createjs.Shape();
    game.gameMap.editFloor.regX = 0;
    game.gameMap.editFloor.regY = 0;
    game.gameMap.editFloor.x = 0;
    game.gameMap.editFloor.y = 0;
    game.gameMap.editFloor.zIndex=0;
    for(var i = 0 ; i < size ; i++){
        var xInit = (-(size/2) * game.gameMap.tileWidth) + (i * game.gameMap.tileWidth / 2);
        var yInit = i * +(game.gameMap.tileHeight/2);
        for(var j = 0 ; j < size ; j++){
            var x = xInit + (j * (game.gameMap.tileWidth / 2));
            var y = yInit - ( j * (game.gameMap.tileHeight / 2));

            game.gameMap.editFloor.graphics
                .moveTo(x, y)
                .beginFill('#00ff00')
                .lineTo(x + (game.gameMap.tileWidth/2), y - (game.gameMap.tileHeight / 2))
                .lineTo(x + game.gameMap.tileWidth, y)
                .lineTo(x + (game.gameMap.tileWidth/2), y + (game.gameMap.tileHeight / 2))
                .lineTo(x , y)
                .endFill();
        }
    }
    game.mainContainer.addChild(game.gameMap.editFloor);
    game.gameMap.updateGrid();
}
gamemap.prototype.cleanInitTiles = function(){
    game.gameMap.editFloor.removeAllEventListeners();
    game.mainContainer.removeAllChildren();
}
gamemap.prototype.update = function(frame){

}
gamemap.prototype.moveToCenter = function(){
    var xReal = window.innerWidth/2;
    var yReal = window.innerHeight/2;
    var xZoom = (xReal * (game.gameMap.zoom * window.innerWidth)) / window.innerWidth;
    var yZoom = (yReal * (game.gameMap.zoom * window.innerHeight)) / window.innerHeight;
    var moveX = xReal - xZoom;
    var moveY = yReal - yZoom;
    game.mainContainer.x = moveX;
    game.mainContainer.y = moveY;
}
/**
 * GRID
 */
gamemap.prototype.displayGrid = function(){
   //On part du principe que 7 est la taille de tuile la plus grosse possible
    var minX = -(7 * game.gameMap.tileWidth / 2);
    var maxX = -minX;
    var maxY = (7 * game.gameMap.tileHeight / 2);
    var minY = -(7 * game.gameMap.tileHeight / 1.4);
    var stepx = $("#grid_scale").val()*game.gameMap.tileWidth;
    var stepy = $("#grid_scale").val()*game.gameMap.tileHeight;

    //On dessine la grille
    console.log([minX,maxX,minY,maxY,stepx,stepy]);
    game.gameMap.editGrid = new createjs.Shape();
    game.gameMap.editGrid.regX = 0;
    game.gameMap.editGrid.regY = 0;
    game.gameMap.editGrid.x = 0;
    game.gameMap.editGrid.y = 0;
    game.gameMap.editGrid.zIndex = 100;
    game.gameMap.editGrid.graphics.beginStroke("#ddd").setStrokeStyle(0.5);
    for (var i = minY; i <= maxY; i = i + stepy) {
        console.log(i);
        game.gameMap.editGrid.graphics
            .moveTo(minX, i)
            .lineTo(maxX, i);
    }
    for(var i = minX; i <= maxX; i = i + stepx){
        game.gameMap.editGrid.graphics
            .moveTo(i, minY)
            .lineTo(i, maxY);
    }
    game.gameMap.editGrid.graphics.endStroke();
    game.mainContainer.addChild(game.gameMap.editGrid);
}
gamemap.prototype.hideGrid = function(){
    if(game.gameMap.editGrid != null){
        game.mainContainer.removeChild(game.gameMap.editGrid);
        delete(game.gameMap.editGrid);
    }
}
gamemap.prototype.updateGrid = function(){
    game.gameMap.hideGrid();
    if($("#Grid_active").is(':checked'))
        game.gameMap.displayGrid();
}
/**
 * CONVERTISER
 */
gamemap.prototype.convertFileCoordsToGridCoords = function(build){
    build.realx = (build.x>>0);
    build.realy = ((build.y * 2)>>0);
    return build;
}
gamemap.prototype.convertGridCoordsToAStarCoords = function(coords){
    var astarX = ((coords[1]>>0) % 2 == 0) ? (((coords[0]>>0) * 2) - 1) : ((coords[0]>>0) * 2);
    return [astarX, coords[1]>>0]
}
gamemap.prototype.convertAStarCoordsToGridCoords = function(coords){
    var Row = coords[1]>>0;
    var Col = (Row % 2 == 0) ? ((coords[0]>>0) + 1)/2 : (coords[0]>>0)/2 ;
    return [Col,Row];
}
gamemap.prototype.convertGridCoordsToPlacementCoords = function(coords){
    var posx = coords[0] * game.gameMap.tileWidth;
    var posy = coords[1] * (game.gameMap.tileHeight/2);
    (coords[1] % 2 != 0) ? posx += (game.gameMap.tileWidth/2) : false;
    return [posx,posy];
}
